package visiopuce.ui;

import java.util.ArrayList;
import java.util.Locale;

import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.nebula.widgets.formattedtext.FormattedText;
import org.eclipse.nebula.widgets.formattedtext.NumberFormatter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import visiopuce.ApplicationContexte;
import visiopuce.objets.Alteration;
import visiopuce.objets.Bande;
import visiopuce.objets.GeneNcbi;
import visiopuce.service.ReferentielServiceImpl;
import visiopuce.ui.providers.BandeLabelProvider;
import visiopuce.ui.providers.GeneNCBILabelProvider;

public class RechercheRegionDialog extends Dialog {

	private ComboViewer comboBande;
	private Combo comboChr;
	private FormattedText tDebut;
	private FormattedText tFin;
	private ComboViewer comboGene;

	public RechercheRegionDialog(Shell parentShell) {

		super(parentShell);

	}

	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText("Rechercher d'une région dans le graphe des puces");
	}

	@Override
	protected Control createDialogArea(Composite parent) {

		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout layout = new GridLayout(3, false);
		layout.marginRight = 5;
		layout.marginLeft = 10;
		container.setLayout(layout);

		Group gpParBande = new Group(container, SWT.NONE);
		gpParBande.setLayout(new GridLayout(2, false));
		gpParBande.setText("Par bande chromosomique");

		Label lblParbande = new Label(gpParBande, SWT.NONE);
		lblParbande.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblParbande.setText("Bande");

		comboBande = new ComboViewer(gpParBande, SWT.DROP_DOWN );
		comboBande.getCombo().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		comboBande.setContentProvider(ArrayContentProvider.getInstance());
		comboBande.setLabelProvider(new BandeLabelProvider());
		ArrayList<Bande> listBandes = ReferentielServiceImpl.getInstance().getBandes();
		// fill the list with ComboValue objects
		comboBande.setInput(listBandes);
		comboBande.addSelectionChangedListener(new ISelectionChangedListener() {

			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				tFin.getControl().setText("");
				tDebut.getControl().setText("");
				comboChr.setText("");
				comboGene.getCombo().setText("");
			}
		});

		Group gpParPosition = new Group(container, SWT.NONE);
		gpParPosition.setLayout(new GridLayout(2, false));
		gpParPosition.setText("Par position génomique (en hg19)");

		Label lblChr = new Label(gpParPosition, SWT.NONE);
		lblChr.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblChr.setText("Chromosome");

		comboChr = new Combo(gpParPosition, SWT.DROP_DOWN );
		comboChr.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		comboChr.setItems(ApplicationContexte.chr);
		comboChr.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				comboGene.getCombo().setText("");
				comboBande.getCombo().setText("");
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub

			}

		});

		Label lblDbut = new Label(gpParPosition, SWT.NONE);
		lblDbut.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblDbut.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblDbut.setText("Début");

		tDebut = new FormattedText(gpParPosition, SWT.BORDER);
		tDebut.getControl().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		tDebut.setFormatter(new NumberFormatter(ApplicationContexte.nfEntier, Locale.FRENCH));
		tDebut.getControl().addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				comboGene.getCombo().setText("");
				comboBande.getCombo().setText("");
			}
		});
		Label lblFin = new Label(gpParPosition, SWT.NONE);
		lblFin.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblFin.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblFin.setText("Fin");

		tFin = new FormattedText(gpParPosition, SWT.BORDER);
		tFin.getControl().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		tFin.setFormatter(new NumberFormatter(ApplicationContexte.nfEntier, Locale.FRENCH));
		tFin.getControl().addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				comboGene.getCombo().setText("");
				comboBande.getCombo().setText("");
			}
		});

		Group gpParGene = new Group(container, SWT.NONE);
		gpParGene.setLayout(new GridLayout(3, false));
		gpParGene.setText("Par gène");

		Label lblParGene = new Label(gpParGene, SWT.NONE);
		lblParGene.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblParGene.setText("Gène");

		comboGene = new ComboViewer(gpParGene, SWT.DROP_DOWN);
		comboGene.getCombo().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		comboGene.setContentProvider(ArrayContentProvider.getInstance());
		comboGene.setLabelProvider(new GeneNCBILabelProvider());
		ArrayList<GeneNcbi> listGenes = ReferentielServiceImpl.getInstance().getGenesNCBI();
		comboGene.setInput(listGenes);
		comboGene.addSelectionChangedListener(new ISelectionChangedListener() {

			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				tFin.getControl().setText("");
				tDebut.getControl().setText("");
				comboChr.setText("");
				comboBande.getCombo().setText("");
			}
		});

		return container;
	}

	// override method to use "Login" as label for the OK button
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, "Rechercher", true);
		createButton(parent, IDialogConstants.CANCEL_ID, "Annuler", false);
	}

	@Override
	protected Point getInitialSize() {
		return new Point(800, 200);
	}

	@Override
	protected void okPressed() {
		Alteration alt = new Alteration();
		alt.setIdAlteration(0);

		ISelection selectionGene = comboGene.getSelection();
		if (!selectionGene.isEmpty() && !comboGene.getCombo().getText().isEmpty()) {
			IStructuredSelection structuredSelection = (IStructuredSelection) selectionGene;
			GeneNcbi gene = ((GeneNcbi) structuredSelection.getFirstElement());
			alt.setDebutHg19(gene.getDebutHg19());
			alt.setFinHg19(gene.getFinHg19());
			alt.setChromosome(gene.getId().getChromosome());
		}

		ISelection selectionBande = comboBande.getSelection();
		if (!selectionBande.isEmpty() && !comboBande.getCombo().getText().isEmpty()) {
			IStructuredSelection structuredSelection = (IStructuredSelection) selectionBande;
			Bande bande = ((Bande) structuredSelection.getFirstElement());
			alt.setDebutHg19(bande.getDebutHg19());
			alt.setFinHg19(bande.getFinHg19());
			alt.setChromosome(bande.getChromosome());
		}

		if (tDebut.getValue() != null && !tDebut.getControl().getText().isEmpty() && tFin.getValue() != null && !tFin.getControl().getText().isEmpty()) {
			alt.setDebutHg19((Long) tDebut.getValue());
			alt.setFinHg19((Long) tFin.getValue());
			alt.setChromosome(comboChr.getText());
		}
		ApplicationContexte.getInstance().setRegionRechercheEnCours(alt);
		super.okPressed();
	}

}
